﻿namespace MhAdmin.SqlSugar;

/// <summary>
/// 仓储模式对象
/// </summary>
[SuppressSniffer]
public partial class DbRepository<T> : SimpleClient<T> where T : class, IPrimaryKey, new()
{
    protected ITenant itenant = null;//多租户事务、GetConnection、IsAnyConnection等功能

    public DbRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
    {
        Context = DbContext.Db.GetConnectionScopeWithAttr<T>();//ioc注入的对象
        itenant = DbContext.Db;
    }

    #region 查询排序拓展

    #region 同步查询

    /// <summary>
    /// 倒叙查询
    /// </summary>
    /// <returns></returns>
    public List<T> GetListDesc()
    {
        return Context.Queryable<T>().OrderByDescending(x => x.Id).ToList();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件)
    /// </summary>
    /// <returns></returns>
    public List<T> GetListDesc(Expression<Func<T, bool>> whereExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(x => x.Id).ToList();
    }

    /// <summary>
    /// 倒叙查询(自定义排序条件)
    /// </summary>
    /// <returns></returns>
    public List<T> GetListDesc(Expression<Func<T, object>> orderExpression)
    {
        return Context.Queryable<T>().OrderByDescending(orderExpression).ToList();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件，自定义排序条件)
    /// </summary>
    /// <returns></returns>
    public List<T> GetListDesc(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(orderExpression).ToList();
    }

    #endregion 同步查询

    #region 异步查询

    /// <summary>
    /// 倒叙查询
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync()
    {
        return Context.Queryable<T>().OrderByDescending(x => x.Id).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(CancellationToken cancellationToken)
    {
        Context.Ado.CancellationToken = cancellationToken;
        return Context.Queryable<T>().OrderByDescending(x => x.Id).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, bool>> whereExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(x => x.Id).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, bool>> whereExpression, CancellationToken cancellationToken)
    {
        Context.Ado.CancellationToken = cancellationToken;
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(x => x.Id).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义排序排序)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, object>> orderExpression)
    {
        return Context.Queryable<T>().OrderByDescending(orderExpression).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义排序排序)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, object>> orderExpression, CancellationToken cancellationToken)
    {
        Context.Ado.CancellationToken = cancellationToken;
        return Context.Queryable<T>().OrderByDescending(orderExpression).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件,自定义排序条件)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(orderExpression).ToListAsync();
    }

    /// <summary>
    /// 倒叙查询(自定义查询条件,自定义排序条件)
    /// </summary>
    /// <returns></returns>
    public Task<List<T>> GetListDescAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, CancellationToken cancellationToken)
    {
        Context.Ado.CancellationToken = cancellationToken;
        return Context.Queryable<T>().Where(whereExpression).OrderByDescending(orderExpression).ToListAsync();
    }

    #endregion 异步查询

    #endregion 查询排序拓展

    #region 仓储方法拓展

    #region 插入

    /// <summary>
    /// 批量插入判断走普通导入还是大数据
    /// </summary>
    /// <param name="data">数据</param>
    /// <param name="threshold">阈值</param>
    /// <returns></returns>
    public virtual async Task<int> InsertOrBulkCopy(List<T> data, int threshold = 10000)
    {
        if (data.Count > threshold)
            return await Context.Fastest<T>().BulkCopyAsync(data);//大数据导入
        else
            return await Context.Insertable(data).ExecuteCommandAsync();//普通导入
    }

    #endregion 插入

    #region 列表

    /// <summary>
    /// 获取列表指定多个字段
    /// </summary>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, T>> selectExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
    }

    /// <summary>
    /// 获取列表指定单个字段
    /// </summary>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<List<string>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
    }

    /// <summary>
    /// 获取列表指定单个字段
    /// </summary>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<List<long>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).ToListAsync();
    }

    #endregion 列表

    #region 单查

    /// <summary>
    /// 获取指定表的单个字段
    /// </summary>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<string> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, string>> selectExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
    }

    /// <summary>
    /// 获取指定表的单个字段
    /// </summary>
    /// <param name="whereExpression">查询条件</param>
    /// <param name="selectExpression">查询字段</param>
    /// <returns></returns>
    public virtual Task<long> GetFirstAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, long>> selectExpression)
    {
        return Context.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
    }

    #endregion 单查

    #endregion 仓储方法拓展
}